package server

import (
	"encoding/json"
	"log"
	"net/http"
)

type StatusHandler struct {
	*HttpServer
}

func (s *StatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	// 根据request type及path具体处理动作
	method := r.Method
	if method != http.MethodGet {
		w.WriteHeader(http.StatusMethodNotAllowed)
		return
	}
	content, err := json.Marshal(s.GetStat())
	if err != nil {
		log.Println(err)
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	_, err = w.Write(content)
	if err != nil {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}
}

func (s *HttpServer) statusHandler() http.Handler {
	return &StatusHandler{s}
}
